﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLiThuVien.Source;
using QuanLiThuVien.LinkModel;

namespace QuanLiThuVien
{
	public partial class TheDocGia
	{
        private InsertInfo m_insertInfo = new InsertInfo();
        private UpdateInfo m_updateInfo = new UpdateInfo();
        private DeleteInfo m_deleteInfo = new DeleteInfo();
        private ShowInfo m_showInfo = new ShowInfo();

        private THUVIEN.Service1 serv = new THUVIEN.Service1();
        static bool m_bInsertData = false;

		public TheDocGia()
		{
			this.InitializeComponent();
            //m_showInfo.ShowListDocGia(dataDocGia);
            addCmbLoaiDG(cmbLoaiDG);
            dtmNgayLap.Text = DateTime.Now.ToString();

		}

        private void addCmbLoaiDG(ComboBox m_cmbTheDocGia)
        {
            for (int i = 0; i < serv.ListTenLoaiDocGia().Length; i++)
                m_cmbTheDocGia.Items.Add(serv.ListTenLoaiDocGia()[i]);
        }

        private void ClearIndex()
        {
            txtMaDocGia.Text = "";
            txtHoTen.Text = "";
            txtDiaChi.Text = "";
            txtEmail.Text = "";
            cmbLoaiDG.SelectedItem = null;
            dtmNgaySinh.SelectedDate = null;
            dtmNgayLap.Text = DateTime.Now.ToString();
        }

        private bool KiemTraNhap()
        {
            if (txtDiaChi.Text == ""
                || txtEmail.Text == ""
                || txtHoTen.Text == ""
                || cmbLoaiDG.SelectedItem == null
                || dtmNgaySinh.SelectedDate == null)
                return true;
            else return false
                ;
        }

        private void RibbntThemDG_Click(object sender, RoutedEventArgs e)
        {
            if (KiemTraNhap())
                MessageBox.Show("Chưa có đầy đủ thông tin để thêm độc giả", "Thông báo");
            else
            {
                string m_sMaLoaiDG = serv.SelectMaLoaiDocGia(cmbLoaiDG.SelectedItem.ToString()).ToString();
                dtmNgayLap.DisplayDate = DateTime.Now;
                DateTime NgayHetHan = dtmNgayLap.DisplayDate.AddMonths(serv.SelectGiaTriThamSo("ThoiHanThe"));
                m_insertInfo.InsertDocGia(m_sMaLoaiDG, txtHoTen.Text, DateTime.Parse(dtmNgaySinh.SelectedDate.ToString()), txtDiaChi.Text, txtEmail.Text, DateTime.Parse(dtmNgayLap.SelectedDate.ToString()), NgayHetHan);
                m_showInfo.ShowListDocGia(dataDocGia);
                ClearIndex();
            }
        }

        //private void RinbntXoaDG_Click(object sender, RoutedEventArgs e)
        //{
        //    var item = dataDocGia.SelectedItem as DSDocGia;
        //    m_deleteInfo.DeleteDocGia(item.MaDocGia);
        //    if (!m_bInsertData)
        //        m_bInsertData = true;
        //    m_showInfo.ShowListDocGia(dataDocGia);
        //}

        private void RibbntSuaDG_Click(object sender, RoutedEventArgs e)
        {
            if (KiemTraNhap())
                MessageBox.Show("Chưa có đầy đủ thông tin để chỉnh sửa", "Thông báo");
            else
            {
                m_bInsertData = true;
                var item = dataDocGia.SelectedItem as DSDocGia;
                string m_sMaLoaiDG = serv.SelectMaLoaiDocGia(cmbLoaiDG.SelectedItem.ToString()).ToString();
                string m_sMaDocGia = item.MaDocGia.ToString();
                DateTime NgayHetHan = dtmNgayLap.DisplayDate.AddMonths(serv.SelectGiaTriThamSo("ThoiHanThe"));
                m_updateInfo.UpdateDocGia(m_sMaDocGia, m_sMaLoaiDG, txtHoTen.Text, DateTime.Parse(dtmNgaySinh.SelectedDate.ToString()), txtDiaChi.Text, txtEmail.Text, DateTime.Parse(dtmNgayLap.SelectedDate.ToString()), NgayHetHan);
                if (!m_bInsertData)
                    m_bInsertData = true;
                ClearIndex();
                dataDocGia.ItemsSource = null;
            }
        }

        private void DataDocGia_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ClearIndex();
            bntThemDG.IsEnabled = false;
            bntTimDocGia.IsEnabled = false;
            if (!m_bInsertData)
            {
                var item = dataDocGia.SelectedItem as DSDocGia;
                txtMaDocGia.Text = item.MaDocGia.ToString();
                txtHoTen.Text = item.HoTenDocGia.ToString();
                txtDiaChi.Text = item.DiaChi.ToString();
                txtEmail.Text = item.Email.ToString();
                cmbLoaiDG.SelectedItem = item.TenLoaiDocGia.ToString();
                dtmNgaySinh.SelectedDate = DateTime.Parse(item.NgaySinh.ToString());
                dtmNgayLap.SelectedDate = DateTime.Parse(item.NgayLapThe.ToString());
            }
            else
            {
                m_bInsertData = false;
                m_showInfo.ShowListDocGia(dataDocGia);
            }
        }

        private void bntTimDocGia_Click(object sender, RoutedEventArgs e)
        {
            dataDocGia.ItemsSource = null;
            m_showInfo.SelectDocGia(dataDocGia,txtHoTen.Text);
            //m_bNotSearch = false;
        }

        private void bntRefresh_Click(object sender, RoutedEventArgs e)
        {
            if (!m_bInsertData)
                m_bInsertData = true;
            ClearIndex();
            dataDocGia.ItemsSource = null;
            bntTimDocGia.IsEnabled = true;
            bntThemDG.IsEnabled = true;
            m_bInsertData = false;
            m_showInfo.ShowListDocGia(dataDocGia);
        }

        private void bntGiaHan_Click(object sender, RoutedEventArgs e)
        {
            if (txtMaDocGia.Text == "")
                MessageBox.Show("Chưa chọn độc giả cần gia hạn thẻ", "Thông báo");
            else
            {
                GiaHanThe m_fGiaHanThe = new GiaHanThe(txtMaDocGia.Text);
                m_fGiaHanThe.Show();
            }
        }

	}
}